﻿using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;

namespace Uber.Collections
{
    /// <summary>
    /// Implements a thread-safe dictionary with multiple readers and one writer
    /// </summary>
    /// <typeparam name="TKey">Key</typeparam>
    /// <typeparam name="TValue">Value</typeparam>
    /// 
    public sealed class SafeDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDisposable
    {
        private readonly Dictionary<TKey, TValue> _inner;
        private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

        public SafeDictionary()
        {
            _inner = new Dictionary<TKey, TValue>();
        }

        public SafeDictionary(int capacity)
        {
            _inner = new Dictionary<TKey, TValue>(capacity);
        }

        public SafeDictionary(SafeDictionary<TKey, TValue> original)
        {
            _inner = new Dictionary<TKey, TValue>(original);
        }

        public void Dispose()
        {
            _lock.Dispose();
        }

        public int Count
        {
            get
            {
                using (RWLSExtension.ReadLock(_lock))
                {
                    return _inner.Count;
                }
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Clear()
        {
            using (RWLSExtension.WriteLock(_lock))
            {
                _inner.Clear();
            }
        }

        public void Add(TKey Item, TValue Value)
        {
            using (RWLSExtension.WriteLock(_lock))
            {
                _inner.Add(Item, Value);
            }
        }

        public void Remove(TKey Item)
        {
            using (RWLSExtension.WriteLock(_lock))
            {
                _inner.Remove(Item);
            }
        }

        public bool ContainsKey(TKey Item)
        {
            using (RWLSExtension.ReadLock(_lock))
            {
                return _inner.ContainsKey(Item);
            }
        }

        public ICollection<TKey> Keys
        {
            get
            {
                using (RWLSExtension.ReadLock(_lock))
                {
                    return new List<TKey>(_inner.Keys);
                }
            }
        }

        public ICollection<TValue> Values
        {
            get
            {
                using (RWLSExtension.ReadLock(_lock))
                {
                    return new List<TValue>(_inner.Values);
                }
            }
        }

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            using (RWLSExtension.ReadLock(_lock))
            {
                return new Dictionary<TKey, TValue>(_inner).GetEnumerator();
            }
        }

        public TValue this[TKey Item]
        {
            get
            {
                using (RWLSExtension.ReadLock(_lock))
                {
                    return _inner[Item];
                }
            }
            set
            {
                using (RWLSExtension.WriteLock(_lock))
                {
                    _inner[Item] = value;
                }
            }
        }

        bool IDictionary<TKey, TValue>.Remove(TKey key)
        {
            using (RWLSExtension.WriteLock(_lock))
            {
                return _inner.Remove(key);
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            using (RWLSExtension.ReadLock(_lock))
            {
                return new Dictionary<TKey, TValue>(_inner).GetEnumerator();
            }
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            throw new NotImplementedException();
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            throw new NotImplementedException();
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            throw new NotImplementedException();
        }
    }
}
